﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Cimbalino.Phone.Toolkit.Helpers;
using Cimbalino.Phone.Toolkit.Services;
using DepSoft.Mobile.WP8.FollwIt.Framework.Interfaces;
using DepSoft.Mobile.WP8.FollwIt.Model;
using FollwItPortable;
using FollwItPortable.Model;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Practices.ServiceLocation;

namespace DepSoft.Mobile.WP8.FollwIt.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : AppCreativityViewModelBase
    {
        private IFollwItClient _follwItClient;
        private ISecurityService _securityService;
        private Security _security;

        private FollwItUser _follwItUser;
        public FollwItUser FollwItUser
        {
            get { return _follwItUser; }
            set
            {
                if (_follwItUser == value)
                    return;

                _follwItUser = value;
                RaisePropertyChanged();
            }
        }

        private ObservableCollection<FollwItUser> _follwItFriends = new ObservableCollection<FollwItUser>();
        public ObservableCollection<FollwItUser> FollwItFriends
        {
            get { return _follwItFriends; }
            set
            {
                if (value == _follwItFriends)
                    return;

                _follwItFriends = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(ISecurityService securityService, IFollwItClient follwItClient, INavigationService navigationService, IMessenger messenger)
            : base(navigationService, messenger)
        {
            _follwItClient = follwItClient;
            _securityService = securityService;

#if !DEBUG
            GoogleAnalytics.EasyTracker.GetTracker().SendView("MainPage");
#endif
            base.ViewLoadedCommand = new RelayCommand(() => ViewLoaded());
        }

        private async Task ViewLoaded()
        {
            this.IsBusy = true;
            _security = await Task.Run(() =>_securityService.GetCredentialsAsync());

#if !DEBUG
            GoogleAnalytics.EasyTracker.GetTracker().SendEvent("API", "GetPublicProfile", _security.UserName, 0);
#endif
            this.FollwItUser = await Task.Run(() => _follwItClient.GetPublicProfileAsync(_security.UserName));
            this.FollwItFriends.Clear();
            foreach (FollwItFriend friend in this.FollwItUser.Friends.Where(item => !item.IsPrivate))
            {
                try
                {
                    FollwItUser follwItUser = await Task.Run(() => _follwItClient.GetPublicProfileAsync(friend.Username));
                    this.FollwItFriends.Add(follwItUser);                    
                }
                catch (Exception e)
                {
                    //When we try to get a public profile of a privat profile member we get an error in the request, just ignore it and continue
                    var t = e.Message;
                }
            }

            //TODO: We need to see how we can cache the friends list! For now we will not refresh the page at each reload
            this.ViewLoadedCommandEnabled = false;

            this.IsBusy = false;
        }
    }
}